home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_emacs.idb / usr / freeware / share / emacs / 19.34 / lisp / scheme.el.z / scheme.el
Lisp/Scheme  |  1998-10-27  |  19KB  |  516 lines

  1. ;;; scheme.el --- Scheme mode, and its idiosyncratic commands.
  2.  
  3. ;; Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Bill Rozas <jinz@prep.ai.mit.edu>
  6. ;; Keywords: languages, lisp
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  22. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. ;; Boston, MA 02111-1307, USA.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; Adapted from Lisp mode by Bill Rozas, jinx@prep.
  28. ;; Initially a query replace of Lisp mode, except for the indentation 
  29. ;; of special forms.  Probably the code should be merged at some point 
  30. ;; so that there is sharing between both libraries.
  31.  
  32. ;;; Code:
  33.  
  34. (defvar scheme-mode-syntax-table nil "")
  35. (if (not scheme-mode-syntax-table)
  36.     (let ((i 0))
  37.       (setq scheme-mode-syntax-table (make-syntax-table))
  38.       (set-syntax-table scheme-mode-syntax-table)
  39.  
  40.       ;; Default is atom-constituent.
  41.       (while (< i 256)
  42.     (modify-syntax-entry i "_   ")
  43.     (setq i (1+ i)))
  44.  
  45.       ;; Word components.
  46.       (setq i ?0)
  47.       (while (<= i ?9)
  48.     (modify-syntax-entry i "w   ")
  49.     (setq i (1+ i)))
  50.       (setq i ?A)
  51.       (while (<= i ?Z)
  52.     (modify-syntax-entry i "w   ")
  53.     (setq i (1+ i)))
  54.       (setq i ?a)
  55.       (while (<= i ?z)
  56.     (modify-syntax-entry i "w   ")
  57.     (setq i (1+ i)))
  58.  
  59.       ;; Whitespace
  60.       (modify-syntax-entry ?\t "    ")
  61.       (modify-syntax-entry ?\n ">   ")
  62.       (modify-syntax-entry ?\f "    ")
  63.       (modify-syntax-entry ?\r "    ")
  64.       (modify-syntax-entry ?  "    ")
  65.  
  66.       ;; These characters are delimiters but otherwise undefined.
  67.       ;; Brackets and braces balance for editing convenience.
  68.       (modify-syntax-entry ?[ "(]  ")
  69.       (modify-syntax-entry ?] ")[  ")
  70.       (modify-syntax-entry ?{ "(}  ")
  71.       (modify-syntax-entry ?} "){  ")
  72.       (modify-syntax-entry ?\| "  23")
  73.  
  74.       ;; Other atom delimiters
  75.       (modify-syntax-entry ?\( "()  ")
  76.       (modify-syntax-entry ?\) ")(  ")
  77.       (modify-syntax-entry ?\; "<   ")
  78.       (modify-syntax-entry ?\" "\"    ")
  79.       (modify-syntax-entry ?' "  p")
  80.       (modify-syntax-entry ?` "  p")
  81.  
  82.       ;; Special characters
  83.       (modify-syntax-entry ?, "_ p")
  84.       (modify-syntax-entry ?@ "_ p")
  85.       (modify-syntax-entry ?# "_ p14")
  86.       (modify-syntax-entry ?\\ "\\   ")))
  87.  
  88. (defvar scheme-mode-abbrev-table nil "")
  89. (define-abbrev-table 'scheme-mode-abbrev-table ())
  90.  
  91. (defun scheme-mode-variables ()
  92.   (set-syntax-table scheme-mode-syntax-table)
  93.   (setq local-abbrev-table scheme-mode-abbrev-table)
  94.   (make-local-variable 'paragraph-start)
  95.   (setq paragraph-start (concat "$\\|" page-delimiter))
  96.   (make-local-variable 'paragraph-separate)
  97.   (setq paragraph-separate paragraph-start)
  98.   (make-local-variable 'paragraph-ignore-fill-prefix)
  99.   (setq paragraph-ignore-fill-prefix t)
  100.   (make-local-variable 'indent-line-function)
  101.   (setq indent-line-function 'scheme-indent-line)
  102.   (make-local-variable 'parse-sexp-ignore-comments)
  103.   (setq parse-sexp-ignore-comments t)
  104.   (make-local-variable 'comment-start)
  105.   (setq comment-start ";")
  106.   (make-local-variable 'comment-start-skip)
  107.   ;; Look within the line for a ; following an even number of backslashes
  108.   ;; after either a non-backslash or the line beginning.
  109.   (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+[ \t]*")
  110.   (make-local-variable 'comment-column)
  111.   (setq comment-column 40)
  112.   (make-local-variable 'comment-indent-function)
  113.   (setq comment-indent-function 'scheme-comment-indent)
  114.   (make-local-variable 'parse-sexp-ignore-comments)
  115.   (setq parse-sexp-ignore-comments t)
  116.   (setq mode-line-process '("" scheme-mode-line-process)))
  117.  
  118. (defvar scheme-mode-line-process "")
  119.  
  120. (defun scheme-mode-commands (map)
  121.   (define-key map "\t" 'scheme-indent-line)
  122.   (define-key map "\177" 'backward-delete-char-untabify)
  123.   (define-key map "\e\C-q" 'scheme-indent-sexp))
  124.  
  125. (defvar scheme-mode-map nil)
  126. (if (not scheme-mode-map)
  127.     (progn
  128.       (setq scheme-mode-map (make-sparse-keymap))
  129.       (scheme-mode-commands scheme-mode-map)))
  130.  
  131. ;;;###autoload
  132. (defun scheme-mode ()
  133.   "Major mode for editing Scheme code.
  134. Editing commands are similar to those of lisp-mode.
  135.  
  136. In addition, if an inferior Scheme process is running, some additional
  137. commands will be defined, for evaluating expressions and controlling
  138. the interpreter, and the state of the process will be displayed in the
  139. modeline of all Scheme buffers.  The names of commands that interact
  140. with the Scheme process start with \"xscheme-\".  For more information
  141. see the documentation for xscheme-interaction-mode.
  142.  
  143. Commands:
  144. Delete converts tabs to spaces as it moves back.
  145. Blank lines separate paragraphs.  Semicolons start comments.
  146. \\{scheme-mode-map}
  147. Entry to this mode calls the value of scheme-mode-hook
  148. if that value is non-nil."
  149.   (interactive)
  150.   (kill-all-local-variables)
  151.   (scheme-mode-initialize)
  152.   (scheme-mode-variables)
  153.   (run-hooks 'scheme-mode-hook))
  154.  
  155. (defun scheme-mode-initialize ()
  156.   (use-local-map scheme-mode-map)
  157.   (setq major-mode 'scheme-mode)
  158.   (setq mode-name "Scheme"))
  159.  
  160. (defvar scheme-mit-dialect t
  161.   "If non-nil, scheme mode is specialized for MIT Scheme.
  162. Set this to nil if you normally use another dialect.")
  163.  
  164. (defun scheme-comment-indent (&optional pos)
  165.   (save-excursion
  166.     (if pos (goto-char pos))
  167.     (cond ((looking-at ";;;") (current-column))
  168.       ((looking-at ";;")
  169.        (let ((tem (calculate-scheme-indent)))
  170.          (if (listp tem) (car tem) tem)))
  171.       (t
  172.        (skip-chars-backward " \t")
  173.        (max (if (bolp) 0 (1+ (current-column)))
  174.         comment-column)))))
  175.  
  176. (defvar scheme-indent-offset nil "")
  177. (defvar scheme-indent-function 'scheme-indent-function "")
  178.  
  179. (defun scheme-indent-line (&optional whole-exp)
  180.   "Indent current line as Scheme code.
  181. With argument, indent any additional lines of the same expression
  182. rigidly along with this one."
  183.   (interactive "P")
  184.   (let ((indent (calculate-scheme-indent)) shift-amt beg end
  185.     (pos (- (point-max) (point))))
  186.     (beginning-of-line)
  187.     (setq beg (point))
  188.     (skip-chars-forward " \t")
  189.     (if (looking-at "[ \t]*;;;")
  190.     ;; Don't alter indentation of a ;;; comment line.
  191.     nil
  192.       (if (listp indent) (setq indent (car indent)))
  193.       (setq shift-amt (- indent (current-column)))
  194.       (if (zerop shift-amt)
  195.       nil
  196.     (delete-region beg (point))
  197.     (indent-to indent))
  198.       ;; If initial point was within line's indentation,
  199.       ;; position after the indentation.  Else stay at same point in text.
  200.       (if (> (- (point-max) pos) (point))
  201.       (goto-char (- (point-max) pos)))
  202.       ;; If desired, shift remaining lines of expression the same amount.
  203.       (and whole-exp (not (zerop shift-amt))
  204.        (save-excursion
  205.          (goto-char beg)
  206.          (forward-sexp 1)
  207.          (setq end (point))
  208.          (goto-char beg)
  209.          (forward-line 1)
  210.          (setq beg (point))
  211.          (> end beg))
  212.        (indent-code-rigidly beg end shift-amt)))))
  213.  
  214. (defun calculate-scheme-indent (&optional parse-start)
  215.   "Return appropriate indentation for current line as scheme code.
  216. In usual case returns an integer: the column to indent to.
  217. Can instead return a list, whose car is the column to indent to.
  218. This means that following lines at the same level of indentation
  219. should not necessarily be indented the same way.
  220. The second element of the list is the buffer position
  221. of the start of the containing expression."
  222.   (save-excursion
  223.     (beginning-of-line)
  224.     (let ((indent-point (point)) state paren-depth desired-indent (retry t)
  225.       last-sexp containing-sexp first-sexp-list-p)
  226.       (if parse-start
  227.       (goto-char parse-start)
  228.     (beginning-of-defun))
  229.       ;; Find outermost containing sexp
  230.       (while (< (point) indent-point)
  231.     (setq state (parse-partial-sexp (point) indent-point 0)))
  232.       ;; Find innermost containing sexp
  233.       (while (and retry (setq paren-depth (car state)) (> paren-depth 0))
  234.     (setq retry nil)
  235.     (setq last-sexp (nth 2 state))
  236.     (setq containing-sexp (car (cdr state)))
  237.     ;; Position following last unclosed open.
  238.     (goto-char (1+ containing-sexp))
  239.     ;; Is there a complete sexp since then?
  240.     (if (and last-sexp (> last-sexp (point)))
  241.         ;; Yes, but is there a containing sexp after that?
  242.         (let ((peek (parse-partial-sexp last-sexp indent-point 0)))
  243.           (if (setq retry (car (cdr peek))) (setq state peek))))
  244.     (if (not retry)
  245.         ;; Innermost containing sexp found
  246.         (progn
  247.           (goto-char (1+ containing-sexp))
  248.           (if (not last-sexp)
  249.           ;; indent-point immediately follows open paren.
  250.           ;; Don't call hook.
  251.           (setq desired-indent (current-column))
  252.         ;; Move to first sexp after containing open paren
  253.         (parse-partial-sexp (point) last-sexp 0 t)
  254.         (setq first-sexp-list-p (looking-at "\\s("))
  255.         (cond
  256.          ((> (save-excursion (forward-line 1) (point))
  257.              last-sexp)
  258.           ;; Last sexp is on same line as containing sexp.
  259.           ;; It's almost certainly a function call.
  260.           (parse-partial-sexp (point) last-sexp 0 t)
  261.           (if (/= (point) last-sexp)
  262.               ;; Indent beneath first argument or, if only one sexp
  263.               ;; on line, indent beneath that.
  264.               (progn (forward-sexp 1)
  265.                  (parse-partial-sexp (point) last-sexp 0 t)))
  266.           (backward-prefix-chars))
  267.          (t
  268.           ;; Indent beneath first sexp on same line as last-sexp.
  269.           ;; Again, it's almost certainly a function call.
  270.           (goto-char last-sexp)
  271.           (beginning-of-line)
  272.           (parse-partial-sexp (point) last-sexp 0 t)
  273.           (backward-prefix-chars)))))))
  274.       ;; If looking at a list, don't call hook.
  275.       (if first-sexp-list-p
  276.       (setq desired-indent (current-column)))
  277.       ;; Point is at the point to indent under unless we are inside a string.
  278.       ;; Call indentation hook except when overridden by scheme-indent-offset
  279.       ;; or if the desired indentation has already been computed.
  280.       (cond ((car (nthcdr 3 state))
  281.          ;; Inside a string, don't change indentation.
  282.          (goto-char indent-point)
  283.          (skip-chars-forward " \t")
  284.          (setq desired-indent (current-column)))
  285.         ((and (integerp scheme-indent-offset) containing-sexp)
  286.          ;; Indent by constant offset
  287.          (goto-char containing-sexp)
  288.          (setq desired-indent (+ scheme-indent-offset (current-column))))
  289.         ((not (or desired-indent
  290.               (and (boundp 'scheme-indent-function)
  291.                scheme-indent-function
  292.                (not retry)
  293.                (setq desired-indent
  294.                  (funcall scheme-indent-function
  295.                       indent-point state)))))
  296.          ;; Use default indentation if not computed yet
  297.          (setq desired-indent (current-column))))
  298.       desired-indent)))
  299.  
  300. (defun scheme-indent-function (indent-point state)
  301.   (let ((normal-indent (current-column)))
  302.     (save-excursion
  303.       (goto-char (1+ (car (cdr state))))
  304.       (re-search-forward "\\sw\\|\\s_")
  305.       (if (/= (point) (car (cdr state)))
  306.       (let ((function (buffer-substring (progn (forward-char -1) (point))
  307.                         (progn (forward-sexp 1) (point))))
  308.         method)
  309.         ;; Who cares about this, really?
  310.         ;(if (not (string-match "\\\\\\||" function)))
  311.         (setq function (downcase function))
  312.         (setq method (get (intern-soft function) 'scheme-indent-function))
  313.         (cond ((integerp method)
  314.            (scheme-indent-specform method state indent-point))
  315.           (method
  316.            (funcall method state indent-point))
  317.           ((and (> (length function) 3)
  318.             (string-equal (substring function 0 3) "def"))
  319.            (scheme-indent-defform state indent-point))))))))
  320.  
  321. (defvar scheme-body-indent 2 "")
  322.  
  323. (defun scheme-indent-specform (count state indent-point)
  324.   (let ((containing-form-start (car (cdr state))) (i count)
  325.     body-indent containing-form-column)
  326.     ;; Move to the start of containing form, calculate indentation
  327.     ;; to use for non-distinguished forms (> count), and move past the
  328.     ;; function symbol.  scheme-indent-function guarantees that there is at
  329.     ;; least one word or symbol character following open paren of containing
  330.     ;; form.
  331.     (goto-char containing-form-start)
  332.     (setq containing-form-column (current-column))
  333.     (setq body-indent (+ scheme-body-indent containing-form-column))
  334.     (forward-char 1)
  335.     (forward-sexp 1)
  336.     ;; Now find the start of the last form.
  337.     (parse-partial-sexp (point) indent-point 1 t)
  338.     (while (and (< (point) indent-point)
  339.         (condition-case nil
  340.             (progn
  341.               (setq count (1- count))
  342.               (forward-sexp 1)
  343.               (parse-partial-sexp (point) indent-point 1 t))
  344.           (error nil))))
  345.     ;; Point is sitting on first character of last (or count) sexp.
  346.     (cond ((> count 0)
  347.        ;; A distinguished form.  Use double scheme-body-indent.
  348.        (list (+ containing-form-column (* 2 scheme-body-indent))
  349.          containing-form-start))
  350.       ;; A non-distinguished form. Use body-indent if there are no
  351.       ;; distinguished forms and this is the first undistinguished
  352.       ;; form, or if this is the first undistinguished form and
  353.       ;; the preceding distinguished form has indentation at least
  354.       ;; as great as body-indent.
  355.       ((and (= count 0)
  356.         (or (= i 0)
  357.             (<= body-indent normal-indent)))
  358.        body-indent)
  359.       (t
  360.        normal-indent))))
  361.  
  362. (defun scheme-indent-defform (state indent-point)
  363.   (goto-char (car (cdr state)))
  364.   (forward-line 1)
  365.   (if (> (point) (car (cdr (cdr state))))
  366.       (progn
  367.     (goto-char (car (cdr state)))
  368.     (+ scheme-body-indent (current-column)))))
  369.  
  370. ;;; Let is different in Scheme
  371.  
  372. (defun would-be-symbol (string)
  373.   (not (string-equal (substring string 0 1) "(")))
  374.  
  375. (defun next-sexp-as-string ()
  376.   ;; Assumes that protected by a save-excursion
  377.   (forward-sexp 1)
  378.   (let ((the-end (point)))
  379.     (backward-sexp 1)
  380.     (buffer-substring (point) the-end)))
  381.  
  382. ;; This is correct but too slow.
  383. ;; The one below works almost always.
  384. ;;(defun scheme-let-indent (state indent-point)
  385. ;;  (if (would-be-symbol (next-sexp-as-string))
  386. ;;      (scheme-indent-specform 2 state indent-point)
  387. ;;      (scheme-indent-specform 1 state indent-point)))
  388.  
  389. (defun scheme-let-indent (state indent-point)
  390.   (skip-chars-forward " \t")
  391.   (if (looking-at "[-a-zA-Z0-9+*/?!@$%^&_:~]")
  392.       (scheme-indent-specform 2 state indent-point)
  393.       (scheme-indent-specform 1 state indent-point)))
  394.  
  395. ;; (put 'begin 'scheme-indent-function 0), say, causes begin to be indented
  396. ;; like defun if the first form is placed on the next line, otherwise
  397. ;; it is indented like any other form (i.e. forms line up under first).
  398.  
  399. (put 'begin 'scheme-indent-function 0)
  400. (put 'case 'scheme-indent-function 1)
  401. (put 'delay 'scheme-indent-function 0)
  402. (put 'do 'scheme-indent-function 2)
  403. (put 'lambda 'scheme-indent-function 1)
  404. (put 'let 'scheme-indent-function 'scheme-let-indent)
  405. (put 'let* 'scheme-indent-function 1)
  406. (put 'letrec 'scheme-indent-function 1)
  407. (put 'sequence 'scheme-indent-function 0)
  408.  
  409. (put 'call-with-input-file 'scheme-indent-function 1)
  410. (put 'with-input-from-file 'scheme-indent-function 1)
  411. (put 'with-input-from-port 'scheme-indent-function 1)
  412. (put 'call-with-output-file 'scheme-indent-function 1)
  413. (put 'with-output-to-file 'scheme-indent-function 1)
  414. (put 'with-output-to-port 'scheme-indent-function 1)
  415.  
  416. ;;;; MIT Scheme specific indentation.
  417.  
  418. (if scheme-mit-dialect
  419.     (progn
  420.       (put 'fluid-let 'scheme-indent-function 1)
  421.       (put 'in-package 'scheme-indent-function 1)
  422.       (put 'let-syntax 'scheme-indent-function 1)
  423.       (put 'local-declare 'scheme-indent-function 1)
  424.       (put 'macro 'scheme-indent-function 1)
  425.       (put 'make-environment 'scheme-indent-function 0)
  426.       (put 'named-lambda 'scheme-indent-function 1)
  427.       (put 'using-syntax 'scheme-indent-function 1)
  428.  
  429.       (put 'with-input-from-string 'scheme-indent-function 1)
  430.       (put 'with-output-to-string 'scheme-indent-function 0)
  431.       (put 'with-values 'scheme-indent-function 1)
  432.  
  433.       (put 'syntax-table-define 'scheme-indent-function 2)
  434.       (put 'list-transform-positive 'scheme-indent-function 1)
  435.       (put 'list-transform-negative 'scheme-indent-function 1)
  436.       (put 'list-search-positive 'scheme-indent-function 1)
  437.       (put 'list-search-negative 'scheme-indent-function 1)
  438.  
  439.       (put 'access-components 'scheme-indent-function 1)
  440.       (put 'assignment-components 'scheme-indent-function 1)
  441.       (put 'combination-components 'scheme-indent-function 1)
  442.       (put 'comment-components 'scheme-indent-function 1)
  443.       (put 'conditional-components 'scheme-indent-function 1)
  444.       (put 'disjunction-components 'scheme-indent-function 1)
  445.       (put 'declaration-components 'scheme-indent-function 1)
  446.       (put 'definition-components 'scheme-indent-function 1)
  447.       (put 'delay-components 'scheme-indent-function 1)
  448.       (put 'in-package-components 'scheme-indent-function 1)
  449.       (put 'lambda-components 'scheme-indent-function 1)
  450.       (put 'lambda-components* 'scheme-indent-function 1)
  451.       (put 'lambda-components** 'scheme-indent-function 1)
  452.       (put 'open-block-components 'scheme-indent-function 1)
  453.       (put 'pathname-components 'scheme-indent-function 1)
  454.       (put 'procedure-components 'scheme-indent-function 1)
  455.       (put 'sequence-components 'scheme-indent-function 1)
  456.       (put 'unassigned\?-components 'scheme-indent-function 1)
  457.       (put 'unbound\?-components 'scheme-indent-function 1)
  458.       (put 'variable-components 'scheme-indent-function 1)))
  459.  
  460. (defun scheme-indent-sexp ()
  461.   "Indent each line of the list starting just after point."
  462.   (interactive)
  463.   (let ((indent-stack (list nil)) (next-depth 0) bol
  464.     outer-loop-done inner-loop-done state this-indent)
  465.     (save-excursion (forward-sexp 1))
  466.     (save-excursion
  467.       (setq outer-loop-done nil)
  468.       (while (not outer-loop-done)
  469.     (setq last-depth next-depth
  470.           innerloop-done nil)
  471.     (while (and (not innerloop-done)
  472.             (not (setq outer-loop-done (eobp))))
  473.       (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
  474.                       nil nil state))
  475.       (setq next-depth (car state))
  476.       (if (car (nthcdr 4 state))
  477.           (progn (indent-for-comment)
  478.              (end-of-line)
  479.              (setcar (nthcdr 4 state) nil)))
  480.       (if (car (nthcdr 3 state))
  481.           (progn
  482.         (forward-line 1)
  483.         (setcar (nthcdr 5 state) nil))
  484.         (setq innerloop-done t)))
  485.     (if (setq outer-loop-done (<= next-depth 0))
  486.         nil
  487.       (while (> last-depth next-depth)
  488.         (setq indent-stack (cdr indent-stack)
  489.           last-depth (1- last-depth)))
  490.       (while (< last-depth next-depth)
  491.         (setq indent-stack (cons nil indent-stack)
  492.           last-depth (1+ last-depth)))
  493.       (forward-line 1)
  494.       (setq bol (point))
  495.       (skip-chars-forward " \t")
  496.       (if (or (eobp) (looking-at "[;\n]"))
  497.           nil
  498.         (if (and (car indent-stack)
  499.              (>= (car indent-stack) 0))
  500.         (setq this-indent (car indent-stack))
  501.           (let ((val (calculate-scheme-indent
  502.               (if (car indent-stack) (- (car indent-stack))))))
  503.         (if (integerp val)
  504.             (setcar indent-stack
  505.                 (setq this-indent val))
  506.                   (if (cdr val)
  507.                       (setcar indent-stack (- (car (cdr val)))))
  508.           (setq this-indent (car val)))))
  509.         (if (/= (current-column) this-indent)
  510.         (progn (delete-region bol (point))
  511.                (indent-to this-indent)))))))))
  512.  
  513. (provide 'scheme)
  514.  
  515. ;;; scheme.el ends here
  516.